<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/12/10
 * Time: 10:28
 */
namespace Admin\Controller;


class SystemController extends RightController
{
    /**
     * 检索系统表
     */
    function selSystem()
    {
        $modules = M('system')->field("module")->group("module")->select();
        $moduleList = array();
        for ($i = 0; $i < count($modules); $i++) {
            $module = $modules[$i]['module'];
            $configs = array();
            $subModules = M('system')->where(array('module' => $module))->select();
            for ($j = 0; $j < count($subModules); $j++) {
                $subModule = $subModules[$j];
                $key = $subModule['code'];
                $configs[$key] = $subModule;
            }
            array_push($moduleList,$configs);
        }
        return $moduleList;
    }

    public function index()
    {
        $list = $this->selSystem();
        $this->assign('list', $list);// 赋值数据集
        $this->display();
    }

    /*编辑类别*/
    public function edit()
    {
        if (IS_AJAX) {
            $key = I("post.code");
            $module = I("post.module");
            $val = I("post.val");
            $where = array(
                'code' => $key,
                'module' => $module
            );
            $data = array(
                'val' => $val
            );

            $keyRes = M("System")->where($where)->save($data);
            if ($keyRes !== false) {
                // 加载系统缓存
                loadSystem();
                $this->ajaxSuccess("系统修改成功！");
            } else {
                $this->ajaxError("系统修改失败！");
            }
        } else {
            $module = I("get.module");

            $key = I("get.code");
            $where = array(
                'module' => $module,
                'code' => $key
            );
            $resKey = M("System")->where($where)->find();
            $this->assign('resKey', $resKey);
            $this->display();
        }
    }

    /*编辑类别*/
    public function aboutEdit()
    {
        $module = I("get.module");
        $key = I("get.code");
        $where = array(
            'module' => $module,
            'code' => $key
        );
        $resKey = M("System")->where($where)->find();
        $this->assign('resKey', $resKey);
        $this->display();
    }
}